<template>
  <div class="auth-page-container register-page">
    <div class="auth-box">
      <div class="auth-image-area">
        <img src="@/assets/styles/second.jpg" alt="Register Image" class="auth-image" />
      </div>
      <div class="auth-form-area">
        <el-card class="auth-card register-card">
          <div slot="header" class="clearfix card-header">
            <span>用户注册</span>
          </div>
          <el-form
            ref="registerForm"
            :model="registerForm"
            :rules="rules"
            label-width="80px"
            @submit.native.prevent
          >
            <el-form-item label="用户名" prop="username">
              <el-input v-model="registerForm.username" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="registerForm.password"
                type="password"
                placeholder="请输入密码"
                show-password
              ></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input
                v-model="registerForm.confirmPassword"
                type="password"
                placeholder="请再次输入密码"
                show-password
              ></el-input>
            </el-form-item>
          </el-form>
          <div class="button-row">
              <el-button type="primary" @click="handleRegister" :loading="loading">注册</el-button>
            </div>
           <div class="auth-links">
             <router-link to="/login" class="create-account-link">返回登录</router-link>
             <!-- 第三方登录图标位置 -->
             <div class="social-icons">
                 <!-- Icons go here -->
             </div>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions } from "vuex";

export default {
  name: "Register",
  data() {
    // 自定义验证规则：确认密码
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      registerForm: {
        username: "",
        password: "",
        confirmPassword: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          { validator: validateConfirmPassword, trigger: "blur" }
        ]
      },
      loading: false
    };
  },
  methods: {
    ...mapActions(["register"]),
    handleRegister() {
      this.$refs.registerForm.validate(async valid => {
        if (valid) {
          this.loading = true;
          try {
            await this.register({
              username: this.registerForm.username,
              password: this.registerForm.password
            });
            this.$message.success("注册成功，请登录");
            this.$router.push("/login");
          } catch (error) {
            this.$message.error(error.message || "注册失败");
          } finally {
            this.loading = false;
          }
        }
      });
    }
  }
};
</script>

<style scoped>
.auth-page-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.auth-box {
  display: flex;
  width: 800px;
  max-width: 90%;
  height: 500px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.auth-image-area {
  flex: 1.2;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  box-sizing: border-box;
  overflow: hidden;
}

.auth-image {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 10px;
}

.auth-form-area {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  box-sizing: border-box;
}

.auth-card {
  width: 100%;
  border: none;
  box-shadow: none;
  display: flex;
  flex-direction: column;
}

.card-header {
  text-align: center;
  font-size: 1.2em;
  margin: 0;
  padding: 5px 0;
  border-bottom: none;
}

.register-form {
    flex-grow: 1;
}

.auth-form-area .el-form-item {
  margin-bottom: 20px;
}

.button-row {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  margin-bottom: 20px;
  width: 100%;
}

.button-row .el-button {
    width: 100%;
}

.auth-links {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 0;
  border-top: none;
  gap: 10px;
}

.create-account-link {
  text-decoration: none;
  color: #409eff;
  font-size: 0.9em;
}

.create-account-link:hover {
  text-decoration: underline;
}

.social-icons {
  /* Styles for social icons container */
}
</style>
